using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DailyTransaction
{
    public partial class frmCreatCOMP : Form
    {
        private BAO_Rcpt bao = new BAO_Rcpt();
        private string comp_code = "";
        private string comp_name = "";
        public int role;
        public frmCreatCOMP()
        {
            InitializeComponent();
        }

        private void btnGetComcod_Click(object sender, EventArgs e)
        {
            ChooseDataForm cform = new ChooseDataForm();
            cform.ShowDialog();

            txtCompCod.Text = cform.comp_code;
            comp_code = cform.comp_code;
            comp_name = cform.comp_name;
            if (txtCompCod.Text.Trim() == "")
                return;
            getData(txtCompCod.Text.Trim());
        }

        private void getData(string comp_code)
        {
            clearData();
            DataTable tbl = bao.getData(comp_code);

            if (tbl != null && tbl.Rows.Count > 0)
            {
                DataRow r = tbl.Rows[0];
                txtCompSUNC.ReadOnly = true;
                txtCompName.Text = r["COMP_NAME"].ToString();
                txtKindPmnt.Text = r["KIND_PMENT"].ToString();
                txtCompSUNC.Text = r["COMP_SUNCD"].ToString();
                txtDisp.Text = r["ACC_CRE_DISP"].ToString();
                txtDrug.Text = r["ACC_CRE_DRUG"].ToString();
                txtMed.Text = r["ACC_CRE_MED"].ToString();
                txtServ.Text = r["ACC_CRE_SERV"].ToString();
                dtpCreateDate.Value = r["COMP_CREAT"] != null ? DateTime.Parse(r["COMP_CREAT"].ToString()) : DateTime.Now;
                txtT0.Text = r["T0_code"].ToString();
                txtT4.Text = r["T4_code"].ToString();
                chkAda.Checked = r["ADA"] != null ? bool.Parse(r["ADA"].ToString()) : false;
                chkSama.Checked = r["SAMA"] != null ? bool.Parse(r["SAMA"].ToString()) : false;
            }
            else
            {
                txtCompSUNC.ReadOnly = false;
                txtCompName.Text = comp_name;
                txtMed.Text = "4010101";
                txtServ.Text = "";
                txtDisp.Text = "4010202";
                txtDrug.Text = "4010201";
            }
        }
        private void clearData()
        {
            txtCompName.Text = "";
            txtKindPmnt.Text = "";
            txtCompSUNC.Text = "";
            txtDisp.Text = "";
            txtDrug.Text = "";
            txtMed.Text = "";
            txtServ.Text = "";
            dtpCreateDate.Value = DateTime.Now;
            txtT0.Text = "";
            txtT4.Text = "";
            chkAda.Checked = false;
            chkSama.Checked = false;
        }              

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtCompCod_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab)
                getData(txtCompCod.Text.Trim());
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtCompCod.Text.Trim() == "")
            {
                MessageBox.Show("Comp_code mustn't be blank ");
                txtCompCod.Focus();
                return;
            }
            if (txtCompName.Text.Trim() == "")
            {
                MessageBox.Show("Company name mustn't be blank");
                return;
            }
            if (txtCompSUNC.Text.Trim() == "")
            {
                MessageBox.Show("CompSUNC mustn't be blank");
                txtCompSUNC.Focus();
                return;
            }
            if (txtKindPmnt.Text.Trim() == "")
            {
                MessageBox.Show("Kind payment mustn't be blank");
                txtKindPmnt.Focus();
                return;
            }
            DataTable tbl = bao.getData(txtCompCod.Text.Trim());
            int num = 0;
            //if (tbl.Rows.Count > 0)
            //{
            //    num = bao.update_data(txtCompCod.Text, txtCompName.Text, txtCompSUNC.Text,
            //                    txtKindPmnt.Text, txtMed.Text, txtServ.Text, txtT0.Text, txtT4.Text,
            //                    txtDisp.Text, txtDrug.Text, dtpCreateDate.Value.ToShortDateString(),
            //                    chkAda.Checked, chkSama.Checked);
            //}
            
                num = bao.insertNewData(txtCompCod.Text, txtCompName.Text, txtCompSUNC.Text,
                                txtKindPmnt.Text, txtMed.Text, txtServ.Text, txtT0.Text, txtT4.Text,
                                txtDisp.Text, txtDrug.Text, dtpCreateDate.Value.ToShortDateString(),
                                chkAda.Checked, chkSama.Checked);
            
            /*string comp_SUNC = txtCompSUNC.Text.Trim(); //"10401" + txtCompSUNC.Text.Trim();
            int num = bao.insertNewData(txtCompCod.Text, txtCompName.Text, comp_SUNC,
                            txtKindPmnt.Text, "4010101", "", txtT0.Text, txtT4.Text, 
                            "4010202", "4010201",dtpCreateDate.Value.ToShortDateString(), 
                            chkAda.Checked, chkSama.Checked);*/

            //comp_SUNC = 10401 + txtKindPmnt.Text           
            if (num >= 1)
            {
                int k = bao.Update_Kind_Pmnt_Desc();

                MessageBox.Show("Insert new company successful!");
                
            }
            else if (num == -1)
                MessageBox.Show("This company is existed");
            refresh_list();
        }

        private void txtKindPmnt_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Tab || e.KeyData == Keys.Enter)
                txtKindPmnt.Text = txtKindPmnt.Text.ToUpper();
        }

        private void txtKindPmnt_Leave(object sender, EventArgs e)
        {
            txtKindPmnt.Text = txtKindPmnt.Text.ToUpper();
        }

        private void txtCompSUNC_Leave(object sender, EventArgs e)
        {
            txtCompSUNC.Text = txtCompSUNC.Text.ToUpper();
        }

        private void txtCompSUNC_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Tab || e.KeyData == Keys.Enter)
                txtCompSUNC.Text = txtCompSUNC.Text.ToUpper();
        }

        private void frmCreatCOMP_Load(object sender, EventArgs e)
        {
            txtMed.Text = "4010101";
            txtServ.Text = "";
            txtDisp.Text = "4010202";
            txtDrug.Text = "4010201";
            lstComp.DataSource = bindingSource1;
            refresh_list();
        }
        private void refresh_list()
        {
            DataTable tbl = bao.get_lst_company();
            bindingSource1.DataSource = tbl;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtCompCod.Text = "";
            txtCompName.Text = "";
            txtCompSUNC.Text = "";
            txtDisp.Text = "";
            txtDrug.Text = "";
            txtKindPmnt.Text = "";
            txtMed.Text = "";
            txtServ.Text = "";
            txtT0.Text = "";
            txtT4.Text = "";
        }              
    }
}